package quq.missq.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.photoselector.model.PhotoModel;
import com.photoselector.ui.PhotoSelectorActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import quq.missq.BaseActivity;
import quq.missq.Constants;
import quq.missq.R;
import quq.missq.adapter.PublishTopicImageAdapter;
import quq.missq.beans.Author;
import quq.missq.utils.BitmapUtils;
import quq.missq.utils.HttpRequest;
import quq.missq.utils.ImageUploadAsyncTask;
import quq.missq.utils.ImageUtil;
import quq.missq.utils.UserTools;
import quq.missq.utils.VolleyTool;
import quq.missq.views.NoScrollGridView;

/**
 * Created by River on 8/10/15.
 */
public class PublishComentActivity extends BaseActivity implements View.OnClickListener,VolleyTool.HTTPListener{

    private TextView mCancel;
    private TextView mSend;
    private EditText mContent;
    private ImageView mAtPeople;
    private ImageView mChoosePic;
    private TextView mAtContent;
    private String atContent = "";
    private ProgressDialog dialog;
    private String atUserIds = "";
    private int targetType = -1;   //1大招 2话题
    private int targetId = -1;//大招或话题Id
    private long parentId = 0l; // 回复评论ID
    private long userId = 0l; //回复userid
    private String content = "";
    private String reUserName = "";// 在提交成功后，用于返回到前一个页面刷新数据
    private ImageView mIcon;
    private String mPath = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.publish_comment_activity);
        Bundle bundle = getIntent().getExtras();
        targetType = bundle.getInt("targetType");
        targetId = bundle.getInt("targetId");
        parentId = bundle.getInt("parentId",0);
        userId = bundle.getInt("userId",0);
        reUserName = bundle.getString("reUserName");
        initView();
        initListener();
        initData();
    }

    private void initView(){
        mIcon = (ImageView)findViewById(R.id.icon);
        mCancel = (TextView)findViewById(R.id.cancel);
        mSend = (TextView)findViewById(R.id.send);
        mContent = (EditText)findViewById(R.id.content);
        mAtPeople = (ImageView)findViewById(R.id.atPeople);
        mChoosePic = (ImageView)findViewById(R.id.choosePic);
        mAtContent = (TextView)findViewById(R.id.atContent);
    }

    private void initListener(){
        mCancel.setOnClickListener(this);
        mSend.setOnClickListener(this);
        mAtPeople.setOnClickListener(this);
        mChoosePic.setOnClickListener(this);
    }

    private void initData(){
    }

    private static final int CHOOSE_PIC = 0;
    private static final int CHOOSE_PEOPLE = 1;

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()){
            case R.id.cancel:
                this.finish();
                break;
            case R.id.send:
                send();
                break;
            case R.id.atPeople:
                intent = new Intent(this, MyContractorActivity.class);
                startActivityForResult(intent, CHOOSE_PEOPLE);
                break;
            case R.id.choosePic:
                intent = new Intent(this, SelectPicActivity.class);
                intent.putExtra(SelectPicActivity.KEY_CUT_X,1);
                intent.putExtra(SelectPicActivity.KEY_CUT_Y,1);
                startActivityForResult(intent, CHOOSE_PIC);
                break;
        }
    }

    private void send(){
        content = mContent.getText().toString();
        if(TextUtils.isEmpty(content)){
            showToast("评论不能为空");
            return;
        }
        String acc_token = UserTools.getUser(this).getAcc_token();
        Map<String,String> params = new HashMap<>();
        params.put("targetType",targetType+"");
        params.put("content",content);
        params.put("atUserIds",atUserIds);
        params.put("acc_token",acc_token);
        params.put("targetId",targetId+"");
        if(userId != 0){
            params.put("userId",userId+"");
        }
        if(parentId != 0){
            params.put("parentId",parentId+"");
        }
        dialog = ProgressDialog.show(this, "提示", "提交中...", false, false);
        Map<String,File> files = new HashMap<>();
        if(!"".equals(mPath)){
            File file = new File(mPath);
            files.put("pic",file);
        }
        ImageUploadAsyncTask imageUploadAsyncTask = new ImageUploadAsyncTask(params,files, Constants.ADD_COMMENT_URL,new HttpRequest.HttpRequestListener() {
            @Override
            public void httpError() {
                if(dialog!=null){dialog.dismiss();}
                showToast("发布失败，请重试");
            }

            @Override
            public void httpSuccess(String msg) {
                if(dialog!=null){dialog.dismiss();}
                try {
                    JSONObject obj = new JSONObject(msg);
                    int code = obj.getInt("code");
                    String message = obj.getString("message");
                    if(code >= 0){
                        setResult(RESULT_OK);
                        showToast("评论成功");
                        PublishComentActivity.this.finish();
                    }else{
                        showToast(message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },"file");
        imageUploadAsyncTask.execute();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == CHOOSE_PIC) {
            mPath = data.getStringExtra(SelectPicActivity.KEY_PHOTO_PATH);
            mIcon.setImageBitmap(ImageUtil.readFile(mPath));
            mIcon.setVisibility(View.VISIBLE);
        }else if(resultCode == RESULT_OK && requestCode == CHOOSE_PEOPLE){
            Author author = (Author)data.getSerializableExtra("author");
            if("".equals(atContent)){
                atUserIds = author.getId()+"";
                atContent = "@"+author.getNkname();
            }else{
                atUserIds = atUserIds+","+author.getId();
                atContent = atContent+",@"+author.getNkname();
            }
            mAtContent.setText(atContent);
        }
    }

    @Override
    public <T> void onResponse(T t, int requestCode) {

    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {

    }

}
